export function debounce(event: any, wait: number, flag = true) {
  let timer: any = null;
  return function (...args) {
    timer && clearTimeout(timer);
    if (!timer && flag) {
      event.apply(this, args);
      flag = false;
    } else {
      timer = setTimeout(() => {
        event.apply(this, args);
        timer = null;
      }, wait);
    }
  };
}
const toString = Object.prototype.toString;

export function is(val, type) {
  return toString.call(val) === `[object ${type}]`;
}

export function isDef(val) {
  return typeof val !== 'undefined';
}

export function isUnDef(val) {
  return !isDef(val);
}

export function isEmpty(val) {
  if (isArray(val) || isString(val)) {
    return val.length === 0;
  }

  if (val instanceof Map || val instanceof Set) {
    return val.size === 0;
  }

  if (isObject(val)) {
    return Object.keys(val).length === 0;
  }

  return false;
}

export function isSetObject(params) {
  return is(params, 'Set');
}

export function isObject(params) {
  return is(params, 'Object');
}

export function isDate(val) {
  return is(val, 'Date');
}

export function isNull(val) {
  return val === null;
}

export function isNullAndUnDef(val) {
  return isUnDef(val) && isNull(val);
}

export function isNullOrUnDef(val) {
  return isUnDef(val) || isNull(val);
}

export function isNumber(val) {
  return is(val, 'Number');
}

export function isPromise(val) {
  return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch);
}

export function isString(val) {
  return is(val, 'String');
}

export function isFunction(val) {
  return is(val, 'Function');
}

export function isBoolean(val) {
  return is(val, 'Boolean');
}

export function isRegExp(val) {
  return is(val, 'RegExp');
}

export function isArray(val) {
  return is(val, 'Array');
}
/**
 * 验证十进制数字
 */
export function number(value: string) {
  return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
}